﻿namespace PhotobucketNet
{
    using System;
    using System.IO;
    using System.Xml;

    public class MediaItem
    {
        protected string _browserUrl;
        protected string _description;
        private const string _description_id = "description_id";
        protected string _descriptionId;
        private const string _image = "image";
        protected bool _isPublic;
        private Stream _mediaStream;
        private MediaTagList _mediaTags;
        protected PhotobucketNet.MediaType _mediaType;
        private PhotobucketNet.MetaData _metaData;
        protected string _name;
        private const string _nameXml = "name";
        private const string _public = "public";
        protected string _thumbUrl;
        protected string _title;
        private const string _typeXml = "type";
        private const string _uploaddate = "uploaddate";
        protected DateTime _uploadDate;
        protected string _url;
        protected string _username;
        private const string _usernameXml = "username";
        private const string _video = "video";

        private MediaItem(PhotobucketNet.MediaType type)
        {
            this._descriptionId = string.Empty;
            this._name = string.Empty;
            this._username = string.Empty;
            this._mediaType = PhotobucketNet.MediaType.None;
            this._isPublic = true;
            this._browserUrl = string.Empty;
            this._url = string.Empty;
            this._thumbUrl = string.Empty;
            this._title = string.Empty;
            this._description = string.Empty;
            this._mediaTags = new MediaTagList();
            this._mediaType = type;
        }

        internal MediaItem(XmlDocument mediaXml)
        {
            this._descriptionId = string.Empty;
            this._name = string.Empty;
            this._username = string.Empty;
            this._mediaType = PhotobucketNet.MediaType.None;
            this._isPublic = true;
            this._browserUrl = string.Empty;
            this._url = string.Empty;
            this._thumbUrl = string.Empty;
            this._title = string.Empty;
            this._description = string.Empty;
            this._mediaTags = new MediaTagList();
            XmlNode mediaNode = mediaXml.SelectSingleNode("descendant::content");
            this.ParseFromXmlNode(mediaNode);
        }

        private MediaItem(string username, PhotobucketNet.MediaType type)
        {
            this._descriptionId = string.Empty;
            this._name = string.Empty;
            this._username = string.Empty;
            this._mediaType = PhotobucketNet.MediaType.None;
            this._isPublic = true;
            this._browserUrl = string.Empty;
            this._url = string.Empty;
            this._thumbUrl = string.Empty;
            this._title = string.Empty;
            this._description = string.Empty;
            this._mediaTags = new MediaTagList();
            this._username = username;
            this._mediaType = type;
        }

        public MediaItem(string username, string name, Stream mediaStream, PhotobucketNet.MediaType type)
        {
            this._descriptionId = string.Empty;
            this._name = string.Empty;
            this._username = string.Empty;
            this._mediaType = PhotobucketNet.MediaType.None;
            this._isPublic = true;
            this._browserUrl = string.Empty;
            this._url = string.Empty;
            this._thumbUrl = string.Empty;
            this._title = string.Empty;
            this._description = string.Empty;
            this._mediaTags = new MediaTagList();
            this._username = username;
            this._name = name;
            this._mediaStream = mediaStream;
            this._mediaType = type;
        }

        public static MediaItem CreateFromFile(string username, string filepath, PhotobucketNet.MediaType type)
        {
            string str;
            FileStream mediaStream = new FileStream(filepath, FileMode.Open);
            if (filepath.LastIndexOf('/') == -1)
            {
                str = filepath.Substring(filepath.LastIndexOf('\\') + 1);
            }
            else
            {
                str = filepath.Substring(filepath.LastIndexOf('/') + 1);
            }
            return new MediaItem(username, str, mediaStream, type);
        }

        public static MediaItem CreateFromUrl(string url, PhotobucketNet.MediaType type)
        {
            string name = url.Substring(url.LastIndexOf('/') + 1);
            int index = url.IndexOf("/albums/");
            if (index == -1)
            {
                throw new PhotobucketApiException("media item url does not have have albums in it");
            }
            string str2 = url.Substring(index + 8);
            int startIndex = str2.IndexOf('/') + 1;
            int num3 = str2.IndexOf('/', startIndex);
            MediaItem item = new MediaItem(url.Substring(startIndex, num3 - startIndex), name, null, type);
            item._url = url;
            item._browserUrl = url;
            return item;
        }

        internal static MediaItem CreateFromXmlNode(XmlNode mediaNode)
        {
            return CreateFromXmlNode(PhotobucketNet.MediaType.None, mediaNode);
        }

        internal static MediaItem CreateFromXmlNode(PhotobucketNet.MediaType type, XmlNode mediaNode)
        {
            MediaItem item = new MediaItem(type);
            item.ParseFromXmlNode(mediaNode);
            return item;
        }

        public override bool Equals(object obj)
        {
            MediaItem item = obj as MediaItem;
            return ((this.Name.Equals(item.Name) && this.Type.Equals(item.Type)) && this.Username.Equals(item.Username));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        protected void ParseFromXmlNode(XmlNode mediaNode)
        {
            foreach (XmlAttribute attribute in mediaNode.Attributes)
            {
                if (attribute.Name == "name")
                {
                    this._name = attribute.Value;
                    continue;
                }
                if (attribute.Name == "description_id")
                {
                    this._descriptionId = attribute.Value;
                    continue;
                }
                if (attribute.Name == "public")
                {
                    if (attribute.Value == "0")
                    {
                        this._isPublic = false;
                    }
                    continue;
                }
                if (attribute.Name == "type")
                {
                    if (attribute.Value == "video")
                    {
                        this._mediaType = PhotobucketNet.MediaType.Video;
                    }
                    else if (attribute.Value == "image")
                    {
                        this._mediaType = PhotobucketNet.MediaType.Image;
                    }
                    continue;
                }
                if (attribute.Name == "uploaddate")
                {
                    if (attribute.Value == string.Empty)
                    {
                        break;
                    }
                    double num = Convert.ToDouble(attribute.Value);
                    int hours = Convert.ToInt32((double) (num / 3600.0));
                    int minutes = Convert.ToInt32((double) ((num % 3600.0) / 60.0));
                    int seconds = Convert.ToInt32((double) ((num % 3600.0) % 60.0));
                    TimeSpan span = new TimeSpan(hours, minutes, seconds);
                    this._uploadDate = new DateTime(0x7b2, 1, 1, 0, 0, 0, 0).Add(span);
                }
                else if (attribute.Name == "username")
                {
                    this._username = attribute.Value;
                }
            }
            XmlNode node = mediaNode.SelectSingleNode("descendant::browseurl");
            this._browserUrl = node.InnerText;
            XmlNode node2 = mediaNode.SelectSingleNode("descendant::url");
            this._url = node2.InnerText;
            XmlNode node3 = mediaNode.SelectSingleNode("descendant::thumb");
            this._thumbUrl = node3.InnerText;
            XmlNode node4 = mediaNode.SelectSingleNode("descendant::title");
            this._title = node4.InnerText;
            XmlNode node5 = mediaNode.SelectSingleNode("descendant::description");
            this._description = node5.InnerText;
        }

        public string BrowserUrl
        {
            get
            {
                return this._browserUrl;
            }
        }

        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }

        public string DescriptionId
        {
            get
            {
                return this._descriptionId;
            }
            set
            {
                this._descriptionId = value;
            }
        }

        public bool IsPublic
        {
            get
            {
                return this._isPublic;
            }
        }

        public Stream MediaStream
        {
            get
            {
                return this._mediaStream;
            }
            set
            {
                this._mediaStream = value;
            }
        }

        public MediaTagList MediaTags
        {
            get
            {
                return this._mediaTags;
            }
        }

        public PhotobucketNet.MediaType MediaType
        {
            get
            {
                return this._mediaType;
            }
        }

        public PhotobucketNet.MetaData MetaData
        {
            get
            {
                return this._metaData;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

        internal MediaTagList SetMediaTags
        {
            set
            {
                this._mediaTags = value;
            }
        }

        internal PhotobucketNet.MetaData SetMetaData
        {
            set
            {
                this._metaData = value;
            }
        }

        public string ThumbUrl
        {
            get
            {
                return this._thumbUrl;
            }
        }

        public string Title
        {
            get
            {
                return this._title;
            }
            set
            {
                this._title = value;
            }
        }

        public string Type
        {
            get
            {
                if (this._mediaType == PhotobucketNet.MediaType.Image)
                {
                    return "image";
                }
                if (this._mediaType != PhotobucketNet.MediaType.Video)
                {
                    throw new PhotobucketApiException("unknow mediatype");
                }
                return "video";
            }
            set
            {
                if (value == "image")
                {
                    this._mediaType = PhotobucketNet.MediaType.Image;
                }
                else
                {
                    if (value != "video")
                    {
                        throw new PhotobucketApiException("unknow mediatype");
                    }
                    this._mediaType = PhotobucketNet.MediaType.Video;
                }
            }
        }

        public DateTime UploadDate
        {
            get
            {
                return this._uploadDate;
            }
        }

        public string Url
        {
            get
            {
                return this._url;
            }
            set
            {
                this._url = value;
            }
        }

        public string Username
        {
            get
            {
                return this._username;
            }
        }
    }
}

